package zhongbo.java.air.ingestion.listener;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;
import zhongbo.java.air.ingestion.dto.FlightPathDto;

import java.util.Date;

/**
 * 接收订阅频道log_chanel 发布的广播消息
 */
@Component
@Slf4j
public class RedisSubscribeChanelListener implements MessageListener {
    @Autowired
    private RedisPublisher publisher;

    /**
     * 消息处理方法
     *
     * @param message 消息内容对象
     * @param chanel  字节数组方式的频道名称
     */
    @Override
    public void onMessage(Message message, byte[] chanel) {
        byte[] messageBody = message.getBody();
        String msgString = new String(messageBody);
        log.info("收到日志消息 {}", msgString);
        doConvert(msgString);

    }

    //字段映射
    private void doConvert(String msgString) {
        JSONObject jsonObject = JSONUtil.parseObj(msgString);


        FlightPathDto flightPath = new FlightPathDto();
        //数组
        JSONArray traceArray = jsonObject.getJSONArray("trace");
        flightPath.setIcaoCode(jsonObject.getStr("icao"));

        // 获取timestamp
        Long timestamp = jsonObject.getLong("timestamp");
        Object o = traceArray.get(0);
        double finalTempStamp = 0;
        if (o != null) {
            //先转换成字符串，再转换成double
            double offset = Double.parseDouble(String.valueOf(o));
            finalTempStamp = timestamp + offset;
            String timeStr = String.valueOf(finalTempStamp); // 使用 String.valueOf() 方法
            double time = Double.parseDouble(timeStr);
            if (timeStr.length() == 13) {
                // 如果长度为13，说明已经是毫秒单位，直接使用
                timestamp = (long) time;
                Date flightPathTimeStamp = new Date(timestamp);
                flightPath.setTimestamp(flightPathTimeStamp);
            } else {
                // 否则，假设是秒单位，需要乘以1000转换为毫秒单位
                timestamp = (long) time;
                Date flightPathTimeStamp = new Date(timestamp * 1000);
                flightPath.setTimestamp(flightPathTimeStamp);
            }

        }


        // 获取sourceType
        Object o1 = traceArray.get(8);
        if (o1 != null) {
            //判断类型
            if (o1 instanceof JSONObject) {
                JSONObject jo = (JSONObject) o1;
                String type = jo.getStr("type");
                flightPath.setSourceType(type);
            }
            // 不是对象，就不处理
        }

        Object latitude = traceArray.get(1);
        Object longitude = traceArray.get(2);
        Object altitude = traceArray.get(3);
        Object speed = traceArray.get(4);
        Object heading = traceArray.get(5);


        if (latitude != null) {
            //强转
            // double o12 = (double)  latitude;

            //先转换成字符串，再转换成double
            double latitude1 = Double.parseDouble(String.valueOf(latitude));
            flightPath.setLatitude(latitude1);
        }
        if (longitude != null) {
            double longitude1 = Double.parseDouble(String.valueOf(longitude));
            flightPath.setLongitude(longitude1);
        }
        if (altitude != null) {
            String str = String.valueOf(altitude);
            double altitude1 = 0;
            if ("ground".equals(str)) {
                //
                altitude1 = 0;
            } else {
                altitude1 = Double.parseDouble(str);
            }
            flightPath.setAltitude(altitude1);
        }
        if (speed != null) {
            double speed1 = Double.parseDouble(String.valueOf(speed));
            flightPath.setSpeed(speed1);
        }
        if (heading != null) {
            double heading1 = Double.parseDouble(String.valueOf(heading));
            flightPath.setHeading(heading1);
        }
        //转换为json字符串
        String jsonMessage = JSONUtil.toJsonStr(flightPath);
//        log.info("转换后的数据: " + jsonMessage);
        // 发送到 injest_flight_path_chen_xiao_qi
        publisher.sendMessage(jsonMessage);
    }
}
